# import gzip
# import json
#
#
# def parse(path):
#     g = gzip.open(path, 'r')
#     for l in g:
#         yield eval(l)
#
#
# if __name__ == '__main__':
#     output_dict = {}
#     for l in parse("test/meta_Video_Games.json.gz"):
#         sid = l["asin"]
#         categories = "|".join(sorted(l["categories"][0]))
#         output_dict[sid] = categories
#
#     context_set = set(list(output_dict.values()))
#     print("context_set length: ", len(context_set))
#     context = {g: i for i, g in enumerate(context_set)}
#     print(context)
#     context_map = {sid: context[c] for (sid, c) in output_dict.items()}
#     print(context_map)
#     # with open("test/Beauty_output.json", "w") as f:
#     #     json.dump(output_dict, f)
#     # print("保存完成")
#
#     # f = open("output.strict", "w")
#     # for l in parse2("test/meta_Beauty.json.gz"):
#     #     f.write(l + '\n')
#     # f.close()
#
#     # f = open("output.strict", "r")
#     # data = json.load(f)
#     # f.close()
#     # print(data)



# ==================测试=============================

import pickle

import pandas as pd

with open("Data/preprocessed/ml-1m_min_rating0-min_uc5-min_sc5-splitleave_one_out/dataset.pkl", 'rb') as fp:
    data = pickle.load(fp)

genre2index = data["gmap"]

index2genre = {index: genre for genre, index in genre2index.items()}

print(genre2index)
print(index2genre)

print(index2genre[147], index2genre[144])

genre_index_list = [197, 101, 190, 132,  42,  60, 124,  33, 271, 132, 124, 124,  41,
        234,  41, 234, 271, 144, 147, 153, 201,  41, 121,  41, 202, 234,  41,
        147,  41, 144, 271]

genre_item_list = [index2genre[index] for index in genre_index_list]

print(genre_item_list)

label = 2688

sid2index = data["smap"]

index2sid = {index: sid for sid, index in sid2index.items()}
print("label sid: %d" % index2sid[label])

candidates = [2688,  206, 2805, 2806, 3041,   96,  235,   60, 1583, 1691, 1481,   10,
        1169, 1709, 1011,  239, 1793, 1914,  719, 1053, 2980,  678,  687,  475,
        2855, 1797, 1897, 2459,  803, 1278, 1931, 2999, 2033,  157, 2021, 2522,
        1023, 1361, 1165,  950, 2697,  550, 1079, 2137, 1867, 2966,   57, 2296,
         433,  955,  927,   33,  999, 1728, 1557, 1112,  924,  304, 2204, 1015,
        2645, 3059, 1359, 3170, 1002, 2904, 2964,  779,  474,    2, 1012,  254,
         593,  998, 1852,  282, 2748, 2355,  549,  448,  885, 2405, 1466, 2483,
        1070, 1671,  459, 2315, 2723, 2888, 1149,   21, 1910, 3034, 1399, 2312,
        2299,  140,  325, 1328, 2310]

candidates_genre = [index2sid[index] for index in candidates]

context_df = pd.read_csv('Data/ml-1m/movies.dat', sep='::', header=None, encoding="ISO-8859-1", engine='python')
context_df.columns = ['sid', 'title', 'genres']

sids = context_df['sid']
genres = context_df['genres']

sid2genre = {sid: genre for sid, genre in zip(sids, genres)}

print(sid2genre)

candidates_genre = [sid2genre[candidate] for candidate in candidates_genre]
print("candidates_genre", candidates_genre)

predict_res = [10,  885, 2688,  474, 1671,   21, 1165,  549, 1359,  950]
predict_sid = [index2sid[index] for index in predict_res]
print(predict_sid)

predict_genres = [sid2genre[sid] for sid in predict_sid]
print(predict_genres)